//
//  CircumViewController.m
//  CivilianTravel
//
//  Created by xalo on 16/7/12.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "CircumViewController.h"
 
#define kURL @"http://tubu.ibuzhai.com/rest/v1/travelog/recommends?&page_size="
#define kURL2  @"&page="


@interface CircumViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSMutableArray *datasourcearray;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,assign)NSInteger page_size;

@end

@implementation CircumViewController
//懒加载
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight-48) style:UITableViewStylePlain];
        _tableview.dataSource = self;
        _tableview.delegate = self;
    }return _tableview;
}
-(NSMutableArray *)datasourcearray{
    if (!_datasourcearray) {
        _datasourcearray=[NSMutableArray array];
    }return _datasourcearray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestwithpage_size:20 page:1];
//    self.tableview.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
//        
//        
//                [self.datasourcearray removeAllObjects];
//                [self requestwithpage_size:20 page:1];
//    }];
    self.tableview.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.page_size = 20;
        self.page = 1;
        self.page_size++;
        self.page++;
        [self requestwithpage_size:self.page_size page:self.page];
        
        
    }];

    self.automaticallyAdjustsScrollViewInsets = NO;
     [self.view addSubview:self.tableview];
    [self.tableview registerNib:[UINib nibWithNibName:@"scxg" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    
}

#pragma mark  -----------tableviewdelegate----------
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    scxg *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    HotModel *modle = nil;
    modle =self.datasourcearray[indexPath.row];
    [cell setcellwithmodle:modle];
    
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    [tableView tableViewDisplayMessage:@"加载中······" withSourceCount:self.datasourcearray.count];
    return self.datasourcearray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200.f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CicumlistViewController *cicumVC = [[CicumlistViewController alloc]init];
//    cicumVC.ID =[NSString stringWithFormat:[self.datasourcearray[indexPath.row]ID]];
    cicumVC.ID = [self.datasourcearray[indexPath.row]ID];
    [self.navigationController  pushViewController:cicumVC animated:YES];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self scrollViewDidScroll:tableView];
}



//滚动视差;
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSArray *visibleCell = [self.tableview visibleCells];
    for (scxg *cell in visibleCell) {
        
        [cell cellOnTableView:self.tableview didScrollOnView:self.view]; //视差效果
    }
}





//数据请求
-(void)requestwithpage_size:(NSInteger)page_size  page:(NSInteger )page{
    NSString *url = [kURL stringByAppendingString:[NSString stringWithFormat:@"%ld",(long)page_size]];
    url = [url stringByAppendingString:kURL2];
    url = [url stringByAppendingString:[NSString stringWithFormat:@"%ld",(long)page]];
    NSLog(@"%@",url);
    [RequestManager fetchDataWithUrl:url parameter:@{} method:@"GET" compeleted:^(NSData *data) {
        NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
       NSLog(@"%@",dic);
        for (NSDictionary *dict in dic[@"logs"]) {
        HotModel *modle = [[HotModel alloc]init];
            [modle setValuesForKeysWithDictionary:dict];
//            [modle setValuesForKeysWithDictionary:dict[@"created_by"]];
            [self.datasourcearray addObject:modle];
        }
    } updateUI:^{
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
         [self.tableview reloadData];
        
       
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
